﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Imagine;

namespace Carmack
{
    class CHeroCarrying : IHeroState
    {
        public void Update(CHero hero_,GameTime gameTime_)
        {
            Dictionary<eHeroAction,Keys> actionMap = hero_.ActionKeyMap;
            Vector2 velocity = hero_.ActorPhysics.Body.GetLinearVelocity();
            int inputPacket = 0;
            foreach ( eHeroAction action in hero_.ActionKeyMap.Keys )
            {
                switch ( action )
                {
                    case eHeroAction.MoveForward:
                        if (TheInputManager.Instance().IsKeyDown( actionMap[eHeroAction.MoveForward] ) )
                        { 
                            velocity.Y = -hero_.Velocity.Y;               
                            inputPacket  |= (Byte)eGameKeys.ForwardKey;
                        }
                        break;
                    case eHeroAction.MoveBackward:
                        if (TheInputManager.Instance().IsKeyDown( actionMap[eHeroAction.MoveBackward] ) )
                        { 
                            velocity.Y = hero_.Velocity.Y;
                            inputPacket  |= (Byte)eGameKeys.BackwardKey;
                        }
                        break;
                    case eHeroAction.MoveLeft:
                        if (TheInputManager.Instance().IsKeyDown( actionMap[eHeroAction.MoveLeft] ) )
                        { 
                            velocity.X = -hero_.Velocity.X;
                            inputPacket  |= (Byte)eGameKeys.LeftKey;
                        }
                        break;
                    case eHeroAction.MoveRight:
                        if (TheInputManager.Instance().IsKeyDown( actionMap[eHeroAction.MoveRight] ) )
                        { 
                            velocity.X = hero_.Velocity.X;
                            inputPacket  |= (Byte)eGameKeys.RightKey;
                        }
                        break;                  
                }
            }

            hero_.InputPacket = inputPacket;
            hero_.ActorPhysics.Body.SetLinearVelocity(velocity);     
        }

        public void Draw(CHero hero_,GameTime gameTime_)
        {}
        public void KeyUp(CHero hero_,Keys[] upKeys)
        {
           Dictionary<eGameKeys,Keys> controlMap = GameManager.Instance().ControlMap;           
            foreach (Keys key in upKeys)
            {    
                //Cant use Switch Case because of Const issue
                if( key.Equals(controlMap[eGameKeys.ForwardKey]) )
                {
                    hero_.InputPacket &= ~(short)eGameKeys.ForwardKey;
                    hero_.ActorPhysics.Body.SetLinearVelocity(new Vector2(0.0f, 0.0f));
                    hero_.ActorPhysics.Body.SetAngularVelocity(0.0f);
                }
                else if(key.Equals(controlMap[eGameKeys.LeftKey]) )
                {
                    hero_.InputPacket &= ~(short)eGameKeys.LeftKey;
                    hero_.ActorPhysics.Body.SetLinearVelocity(new Vector2(0.0f, 0.0f));
                    hero_.ActorPhysics.Body.SetAngularVelocity(0.0f);                
                }
                else if(key.Equals(controlMap[eGameKeys.BackwardKey]) )
                {
                    hero_.InputPacket &= ~(short)eGameKeys.BackwardKey;
                    hero_.ActorPhysics.Body.SetLinearVelocity(new Vector2(0.0f, 0.0f));
                    hero_.ActorPhysics.Body.SetAngularVelocity(0.0f);
                }
                else if(key.Equals(controlMap[eGameKeys.RightKey]) )
                {
                    hero_.InputPacket &= ~(short)eGameKeys.RightKey;
                    hero_.ActorPhysics.Body.SetLinearVelocity(new Vector2(0.0f, 0.0f));
                    hero_.ActorPhysics.Body.SetAngularVelocity(0.0f);
                }
            }
        }
        public void KeyDown(CHero hero_,Keys[] downKey)
        {
             //One Time Key detection here like Reload,Interact etc.
            Dictionary<eGameKeys,Keys> controlMap = GameManager.Instance().ControlMap;
            foreach (Keys key in downKey)
            {                  
                if( key.Equals(controlMap[eGameKeys.PlaceKey]) )
                {
                    //Place object here
                    hero_.InputPacket |= (short)eGameKeys.PlaceKey;
                }                
            }
        }

        public void MouseButtonDown(CHero hero_,eMouseButtons[] downButton, Vector2 pos) { }
        public void MouseButtonUp(CHero hero_,eMouseButtons[] upButton, Vector2 pos) { }
        public void MouseMove(CHero hero_,Vector2 move)
        {
            Vector2 mouseTransformedPos = Vector2.Transform(TheInputManager.Instance().GetMousePos(), Matrix.Invert(Engine.Instance().Camera.GetTransformation()) );
            hero_.RotationAngle = Utility.VectorToAngle(mouseTransformedPos, hero_.ActorPhysics.Position);
        }
    }
}
